package cn.faifai.tagapi.repository;

import cn.faifai.tagapi.dto.TagSerialize;
import cn.faifai.tagapi.entity.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface TagRepository extends JpaRepository<Tag,Long> {

    Page<Tag> findByParentId(Long parentId, PageRequest of);
    List<Tag> findByParentId(Long parentId);
    @Query("select t from Tag t left join fetch t.IFiles where t.id = :id")
    Optional<TagSerialize> findTagsWithFilesById(Long id);

    int countByParentId(Long tagId);

    /**
     * 根据文件osid查询标签
     */
    @Query("select t from Tag t join t.IFiles f where f.osFileId = :osFileId")
    List<Tag> findByIFilesOsFileId(String osFileId);
}
